<?php
/**
 * Enter description :Config hệ thống gửi mail cho hệ thống
 * @author admin888 <doan.hut@gmail.com >
 */
class CaidatController extends Zend_Controller_Action 
{
	/*
	 * admin888 <doan.hut@gmail.com>
	 * Description : dùng chung cho action
	 */	
	protected  $_lever = 'null';
    public function init()
    {
        
       $auth = Zend_Auth::getInstance();
	  if($auth->hasIdentity()){
			$data = $auth->getIdentity();
			$this->view->name = $data->name;
			$this->view->iddangnhap = $data->uid;
			$this->_lever = $data->leve;
			$this->view->lever = $data->leve;
			$cd = new Model_Caidat();
			$thongbao = $cd->getthongbao($data->uid);
			$this->view->thongbao = $thongbao;
			
						
	   }
       //phần lay out trên
	   $this->view->today =$date = date('m-d-Y H:i:s');
       $reponse = $this->getResponse();
       $reponse->insert('top', $this->view->render('index/top.phtml'));
       $this->view->title = " Cài đặt hệ thống - Email Manager ";
    }
	/**
     * admin888 <doan.hut@gmail.com>
     * Description : check nếu chưa login thì chuyển về trang ngoài
     * Note hàm này mọi người có thể dung chung
     */
	public function preDispatch() {
	    $auth = Zend_Auth::getInstance();
	    if (!$auth->hasIdentity()) {
	        if ($this->_request->getActionName() != 'login') {
	            $this->_redirect('login');
	        }
	    }
	}

    /**
     * admin888 <paven@gmail.com>
     * Enter description :check user login 
     * leve = 1 thi admin se config cho toan bo thanh vien duoc
     * leve = 3 thi chi sua duoc cua user thoi 
     */
    public function indexAction()
    {
   	   $auth = Zend_Auth::getInstance();
	   if($auth->hasIdentity()){
			$data = $auth->getIdentity();
			$leve = $data->leve;
			if ($leve == 1){
				$this->_redirect('caidat/alluser');
			}elseif ($leve == 3){
				$this->_redirect('caidat/oneuser');
			}
		
	   }else{
	   		$this->_redirect('index');
	   } 	
    }
    /**
     * admin888 <pavent@gmail.com>
     * Enter description : cai dat cho 1 user
     */
    public function oneuserAction()
    {
		$auth = Zend_Auth::getInstance();
    	$auth->hasIdentity();
    	$data = $auth->getIdentity();
    	$iduser = $data->uid;
    	//lay id dang nhap
    	$config = new Model_Caidat();
		$thongtin = $config->gethongtin($iduser);
		$this->view->thongtin = $thongtin;
		$this->view->matkhau = base64_decode($thongtin['pass_email']);
		
		//lấy thông tin cá nhân 
		$ttcanhan = $config->getthongtincanhan($iduser);
		$this->view->thongtincanha = $ttcanhan;
		//echo "<pre>"; print_r($ttcanhan);		
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Enter description : bat biến update thong tin gửi mail
     */
    public function updateoneuseAction()
    {
    	$update = new  Model_Caidat();
    	$post = $this->_getAllParams();
    	$ud = $update->updatecaidatemail($post);
    	if ($ud){
    		$this->_redirect('caidat/oneuser');
    	}
    	$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
    }
    /**
     * admin888 <pvennt@gmail.com>
     * update thong tin cai dat email của thanh viên  
     */ 
    public function updaemaltvAction()
    {
        $update = new  Model_Caidat();
        $data = $this->_getAllParams();
       // echo "<pre>"; print_r($data);die();
        $ud = $update->updatecaidatemail($data);
        if($ud){
          $this->_redirect("caidat/caidatuser?id=".$data['idtv']."&iddn=".$data['idnddn']."");  
        }
        $this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Enter description : update thong tin chung cua oneuser 
     */
    public function updatethongtinAction()
    {
    	$update = new Model_Caidat();
    	$data = $this->_getAllParams();
    	$ud = $update->getupdatethongtinchung($data);
     	if($ud){
     	 $this->_redirect('caidat/oneuser');
     	}else{
     	 $this->_redirect('index');
     	}
     	    	
    	$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Description : update thongtin user thanh vien 
     */ 
    public function updatetvAction()
    {
        $update = new Model_Caidat();
    	$data = $this->_getAllParams();
        $ud = $update->getupdatethongtinchung($data);
        if($ud){
            $this->_redirect("caidat/caidatuser?id=".$data['uid']."&iddn=".$data['idnddn']."");
        }
    	$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Enter description :bat bien trong json de thay doi pass
     */
    public function updatepassAction()
    {
    	$update = new Model_Caidat();
    	$dulieu = $this->_getAllParams();
    	$ud = $update->updatepass($dulieu);   	
    	$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
    	
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Enter description : ket noi sptp
     */
    public function ketnoismptAction()
    {
    	$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        $this->_helper->layout->disableLayout(); //disable layout
    	$smtp = new Model_Caidat();
    	$thongso = $this->_getAllParams();
    	$ketnoi = $smtp->getketnoi($thongso);
    	if ($ketnoi){
   	        if ($ketnoi == 1){
                $data = array("xuly" =>1);
           }else{
                $data = array("xuly" =>0);
           }	
        echo $this->_getParam('callback') . '(' . Zend_Json::encode($data) . ')';

       }
       exit;
    }
    /**
     * admin888 <paven@gmail.com>
     * Enter description : cai dat cho all user
     */
    public function alluserAction()
    {
    	$dieukhien = new Model_Caidat();
    	$auth = Zend_Auth::getInstance();
    	$auth->hasIdentity();
    	$data = $auth->getIdentity();
    	$leve = $data->leve;
        $papams = $this->_getAllParams();
		if (isset($papams['keytimkiem'])){
         $tukhoa = trim($papams['keytimkiem']);
		}
		if (isset($papams['id'])){
         $idtk  = trim($papams['id']);
		}
    	if ($leve == 1  ){
               if($tukhoa == ''  && $idtk == "0" ){
                    $data = $dieukhien->getlisthanhvien();
                   // $this->view->thanhvien = $data;
               }else if($idtk != '' && $tukhoa == "0" ){
                    $data = $dieukhien->timkiemone($idtk);
                    ///$this->view->thanhvien = $data;  
                    
               }else{
                    $data = $dieukhien->getlisthanhvientimkiem($tukhoa);
                    
                }
				// phan trang 
				$page = $this->_getParam('page',1); // bắt biến trên trang
				$paginator = Zend_Paginator::factory($data); // gán mảng vào zend_paginator
				$paginator->setCurrentPageNumber($page); // chuyển tới trang
				$paginator->setItemCountPerPage(15); // số bản ghi trên 1 trang
				$this->view->paginator = $paginator; // view số trang 
				// end phân trang               
                $this->view->thanhvien = $paginator;
                   
                
            }else{
    		$this->_redirect('caidat/oneuser');
    	} 
    }
    /**
     * admin888<doan.hut@gmail.com>
     * Enter description : 
     */
    public function khoathanhvienAction()
    {
    	$run = new Model_Caidat();
    	$dulieu = $this->_getAllParams();
    	$thucthi = $run->blocktv($dulieu);
    	$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        $this->_helper->layout->disableLayout(); //disable layout
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Thuc hien kich hoat SMS thành viên bằng tay qua
     */
    public function kichhoathanhviensmsAction()
    {
    	$run = new Model_Caidat();
    	$dulieu = $this->_getAllParams();
    	$unlock = $run->blocktv($dulieu);
    	if ($unlock){
    		$thucthi = $run->kichhoatthanhviensms($dulieu);
    		if($thucthi){
    			$kichhoat = $run->mokhoakichhoat($dulieu);
    		}
    	}
    	$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        $this->_helper->layout->disableLayout(); //disable layout
    	
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Enter description Lấy thoogn tin cua 1 user cần edit
     */
    public function caidatuserAction()
    {
        if ($this->_lever == 1 ){
	    	$modler = new Model_Caidat();
	        $data = $this->_getAllParams();
			$thongtin = $modler->gethongtin($data['id']);
			$this->view->thongtin = $thongtin;
			$this->view->matkhau = base64_decode($thongtin['pass_email']);
	        
			//lấy thông tin cá nhân cua user
			$ttcanhan = $modler->getthongtincanhan($data['id']);
			$this->view->thongtincanha = $ttcanhan;
			// tạo thống kê của user 
			require_once 'scripts/functionchat/include/FusionCharts.php';
			// biểu dồ thu hình cột
	        $thu = new Model_Thudagui();
	        $thumau = new Model_Thumau();
	        $thudagui = $thu->demthu($data['id'],1);
	        $thunhap = $thu->demthu($data['id'],0);
	        $thumaus = $thumau->demthuaum($data['id']);
	        $this->view->thumau = $thumaus;
			$this->view->thudagu = $thudagui;
			$this->view->thunhap = $thunhap;
			// laay data  bieu do lien lien he 
	        $lh = new Model_Lienhe();
	        $nhom = $lh->getnhomluid($data['id']);
	        foreach ($nhom as $key => $value){
	         	$nhom[$key]['tonglienhe'] = $lh->demlienhe($value['id_nlh']);
	        }
	        $this->view->thonhkenhonlh = $nhom;
	        // lấy tổng số file đã upload : 
	        $tongfile = $modler->getongfilethanhvien($data['id']);
	        $this->view->tongfile = $tongfile;
			
        }else{
        	$this->_redirect('index');
        } 
    }
    /**
     * admin888 <doan.hut@gmail.com>
     * Enter description :
     */
    public function updatachukyAction()
    {	
    	$modler = new Model_Caidat();
    	$data = $this->_getAllParams();
    	$update = $modler->updatechuky($data);
    	if ($update){
    		if ($this->_lever == 1){
    			$this->_redirect("caidat/caidatuser/id/".$data['uid']."/iddn/".$data['idnddn']); //tra ve veler = 1
    		}else {
    			$this->_redirect('caidat/oneuser'); // trar ve lever = 3
    		}
    	}
    	else{
    		$this->_redirect('index');
    	} 
    	
    	$this->getHelper('viewRenderer')->setNoRender(); // set khong su dung template
        $this->_helper->layout->disableLayout(); //disable layout
    }
   
    

}